<template>
  <div class="change-class body-bg">
    <div v-if="!initLoading">
      <!-- 订单信息 -->
      <div class="order-content">
        <div class="information">
          <p class="title overflow-one">{{package_name}}</p>
          <div class="sign-items" v-for="(item,index) in classDetail.sub_goods" :key="index">
            <div class="item-cont">
              <img class="feng-mian" :src="item.teacher_photo || '/static/images/user.png'" alt="fengmian">
              <div class="item-cont-c">
                <p class="class-name overflow-one">{{item.goods_name}}</p>
                <p class="class-infor"><img src="/static/images/singUp/singUp-icon1.png" alt="icon">{{item.campus_name}}</p>
                <p class="class-infor"><img src="/static/images/singUp/singUp-icon2.png" alt="icon">{{item.open_date}} 至 {{item.end_date}}</p>
                <p class="class-infor"><img src="/static/images/singUp/singUp-icon3.png" alt="icon">{{item.goods_week}}</p>
              </div>
              <!-- <img class="taocan" src="/static/images/singUp/singUp-icon4.png" alt="套餐"> -->
            </div>
            <div class="items-tips">
              <p class="class-tip">
                <span class="items-tip-biaoqie" v-if="item.is_transfer == '1'">随时插班</span>
                <span class="items-tip-biaoqie" v-if="item.retreat_rule == '1'">随时退班</span>
                <span class="items-tip-biaoqie" v-if="item.is_audition == '1'">免费试听</span>
              </p>
              <div>
                <p class="discount-price">￥{{item.mall_price}}</p>
                <p class="price" v-if="item.mall_cost*1>item.mall_price*1">￥{{item.mall_cost}}</p>
              </div>
            </div>
          </div>
        </div>
        <ul class="price-infor">
          <li>原课时费：<span>￥{{mall_cost}}</span></li>
          <!-- <li>商品费：<span>-￥{{mall_price}}</span></li> -->
          <li>优惠：<span>-￥{{discount_money}}</span></li>
          <li v-if="from == '2'">请假课时抵扣：<span>-￥{{classDetail.deduction_amount}}</span></li>
          <li class="yingfu">应付总额：<span>￥{{payment_price*1}}</span></li>
        </ul>
        <!-- 支付方式 -->
        <div class="pay-cont">
          <p class="title">支付方式</p>
          <van-radio-group :value="payMoument" @change="onChange">
            <div class="pay-items">
              <p class="pay-items-p"><img class="pay-items-img" src="/static/images/singUp/wechat-icon.png" alt="wechat"> 微信支付</p>
              <van-radio name="1"></van-radio>
            </div>
            <div class="pay-items">
              <p class="pay-items-p"><img class="pay-items-img" src="/static/images/singUp/other-icon.png" alt="other"> 其他支付</p>
              <van-radio name="2"></van-radio>
            </div>
          </van-radio-group>
        </div>
      </div>
      <!-- 底部 -->
      <van-submit-bar
        :loading="loadingBtn"
        label="总价："
        :suffix-label="discountText + discount_money"
        decimal-length="0"
        :price="payment_price*100"
        button-text="提交订单"
        @submit="onSubmit"
      />
      <!-- <div class="footer">
        <p class="price-cont">应付：<span class="price">￥{{classDetail.price*1 + classDetail.productPrice*1 - classDetail.discount*1}}</span></p>
        <div class="submit-order-btn">提交订单</div>
      </div> -->
    </div>
    <div v-else class="init-loading"><van-loading type="spinner" color="#1989fa" /></div>
  </div>
</template>

<script>
import store from '@/store/globalStore'
export default {
  data() {
    return {
      from: "", // 记录路由来源 1 报名 2 续费
      initLoading: true, // 初始加载
      goods_id: "", // 商品ID
      renew_goods_id: "", // 续报班级ID
      package_name: "",
      mall_cost: "",
      mall_price: "",
      discount_money: "",
      payment_price: "",
      payMoument: "1", // 支付方式
      loadingBtn: false, // 点击提交，按钮显示loading
      discountText: "已优惠：￥",
      deduction_amount: "",
      classDetail: {}
    }
  },
  onLoad(e) {
    // url中传递了from参数，1 来自报名 2 来自续费 3 套餐
    console.log(e)
    this.from = e.from;
    this.goods_id = e.goods_id;
    this.package_name = e.package_name;
    this.mall_cost = e.mall_cost;
    this.mall_price = e.mall_price;
    this.discount_money = e.discount_money;
    this.payment_price = e.payment_price;
  },
  onShow() {
    this.payMoument = "1";
    this.loadingBtn = false;
    // 从缓存读取套餐信息，用于生成订单
    this.classDetail = this.$store.state.packageList;
    setTimeout(() => {
      this.initLoading = false;
    }, 500);
    if(this.deduction_amount>0){
      this.platform_type = 2
    } else {
      this.platform_type = 1
    }
  },
  methods: {
    // 选择支付方式
    onChange(e) {
      this.payMoument = e.mp.detail;
    },
    // 支付
    onSubmit(from) {
      from = this.from;
      this.loadingBtn = true;
      if(this.payMoument == '1' && from == '1' || this.payMoument == '1' && from == '2' || this.payMoument == '1' && from == '3'){
        // 此处唤醒微信支付
        console.log("唤醒微信支付")
      } else {
        this.$fetch.addOrder({
          real_amount: this.payment_price,
          shopping_cart_ids: this.goods_id,
          is_coupon: 2, // 是否使用优惠券（1、是 2、否）
          is_online: this.payMoument,
          platform_type: this.platform_type,
          renew_goods_id: this.renew_goods_id,
        }).then(res=>{
          wx.redirectTo({
            url: `/pages/submitOrderSuccess/main?from=${from}`
          })
        }).catch(err=>{

        })
      }
    }
  },
}
</script>

<style lang="less" scope>
.change-class{
  width: 100%;
  min-height: 100vh;
  overflow: hidden;
  .order-content{
    width: 100%;
    height: calc(~"100vh - 46px");
    overflow: hidden;
    overflow-y: auto;
    padding-bottom: 70px;
    .information{
      width: 100%;
      margin-bottom: 10px;
      .title{
        width: 100%;
        color: #000;
        font-size: 16px;
        font-weight: bold;
        padding: 22px 18px;
        box-sizing: border-box;
        background: #fff;
      }
      .sign-items{
        width: 100%;
        background: #fff;
        overflow: hidden;
        margin-bottom: 14px;
        .item-cont{
          width: 100%;
          background: #E5F9FF;
          padding: 14px 22px;
          box-sizing: border-box;
          position: relative;
          display: flex;
          align-items: center;
          .feng-mian{
            min-width: 75px;
            width: 75px;
            height: 80px;
            display: block;
            margin-right: 12px;
          }
          .item-cont-c{
            width: 100%;
            overflow: hidden;
          }
          .taocan{
            position: absolute;
            top: 6px;
            right: 12px;
            width: 24px;
            height: 24px;
            display: block;
          }
          .class-name{
            width: 100%;
            font-size: 14px;
            color: #666;
            font-weight: bold;
            margin-bottom: 10px;
          }
          .class-infor{
            width: 100%;
            font-size: 12px;
            color: #999;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            margin-top: 5px;
            img{
              width: 12px;
              height: 12px;
              display: block;
              margin-right: 6px;
            }
          }
        }
        .items-tips{
          width: 100%;
          padding: 10px 12px;
          box-sizing: border-box;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .class-tip{
            &.items-tips{
              width: 100%;
              box-sizing: border-box;
              display: flex;
              align-items: center;
              .items-tip-biaoqie{
                padding: 2px 8px;
                border: 1px solid rgba(0,183,238,1);
                border-radius: 8px;
                font-size: 12px;
                color: #00B7EE;
                margin-right: 12px;
              }
            }
          }
          .items-tip-biaoqie{
            padding: 2px 8px;
            border: 1px solid rgba(0,183,238,1);
            border-radius: 8px;
            font-size: 12px;
            color: #00B7EE;
            margin-left: 12px;
          }
          .price{
            text-decoration:line-through;
            color: #A9A9A9;
            font-size: 14px;
            font-weight: bold;
          }
          .discount-price{
            color: #EE0000;
            font-size: 14px;
            font-weight: bold;
          }
        }
      }
    }
    .price-infor{
      width: 100%;
      padding: 15px 25px 20px 25px;
      box-sizing: border-box;
      background: #fff;
      margin-bottom: 10px;
      li{
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 14px;
        color: #676767;
        font-size: 14px;
        span{
          color: #000;
        }
        &:last-child{
          margin-bottom: 0;
        }
      }
      .yingfu{
        color: #676767;
        font-weight: bold;
        span{
          color: #FF3333;
        }
      }
    }
    .pay-cont{
      width: 100%;
      padding: 15px 25px 20px 25px;
      box-sizing: border-box;
      background: #fff;
      margin-bottom: 10px;
      .title{
        width: 100%;
        font-size: 16px;
        color: #1A1A1A;
        margin-bottom: 20px;
      }
      .pay-items{
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 22px;
        .pay-items-p{
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #1A1A1A;
          .pay-items-img{
            width: 20px;
            height: 20px;
            display: block;
            margin-right: 10px;
          }
        }
      }
    }
  }
  .footer{
    width: 100%;
    height: 46px;
    background: #fff;
    box-shadow:0px 3px 8px 0px rgba(0, 0, 0, 0.42);
    display: flex;
    justify-content: space-between;
    align-items: center;
    .price-cont{
      flex-grow: 1;
      text-align: right;
      margin-right: 10px;
      font-size: 14px;
      .price{
        color: #FF4444;
      }
    }
    .submit-order-btn{
      width: 120px;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      font-size: 16px;
      background: #FF4444;
    }
  }
  // 底部vant组件样式
  .van-submit-bar{
    box-shadow:0px 3px 8px 0px rgba(0, 0, 0, 0.42);
  }
  .van-submit-bar__suffix-label{
    font-size: 14px;
    color: #666;
  }
}
</style>
